package com.colin.app.activity.method.handler;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.app.activity.method.MethodContract;
import com.colin.app.activity.method.MethodPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.library.help.LogHelp;

import java.lang.ref.WeakReference;

/**
 * Demo描述:
 * <p>
 * 示例步骤如下:
 * 1 子线程给子线程本身发送消息
 * 2 收到1的消息后,子线程给主线程发送消息
 * 3 收到2的消息后,主线程给子线程发送消息
 * <p>
 * 为实现子线程给自己本身发送消息,关键还是在于构造Handler时传入的Looper.
 * 在此就传入该子线程自己的Looper即调用Looper.myLooper(),代码如下:
 * Looper.prepare();
 * childHandler=new HandlerTest1(Looper.myLooper());
 * Looper.loop();
 * <p>
 * 所以当mHandlerTest1.sendMessage(message);发送消息时
 * 当然是发送到了它自己的消息队列.
 * <p>
 * 当子线程中收到自己发送的消息后,可继续发送消息到主线程.此时只要注意构造
 * Handler时传入的Handler是主线程的Handler即可,即getMainLooper().
 * 其余没啥可说的.
 * <p>
 * <p>
 * 在主线程处理消息后再发消息到子线程
 * <p>
 * <p>
 * 其实这些线程间发送消息,没有什么;关键还是在于构造Handler时传入谁的Looper.
 */
public class HandlerActivity extends AppActivity implements MethodContract.MethodView {
    private MethodContract.MethodPresenter mMethodPresenter = null;
    private TextView text_handler_message;
    private MyHandler childHandler;
    private MyHandler mainHandler;
    @Override
    protected void onDestroy() {
        if (null != mMethodPresenter) {
            mMethodPresenter.destroy();
            mMethodPresenter = null;
        }
        super.onDestroy();
    }
    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mMethodPresenter = new MethodPresenterImpl(this, this, demoBean);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_handler;
    }

    @Override
    public void initView() {
        this.text_handler_message = (TextView) this.findViewById(R.id.text_handler_message);
    }

    @Override
    public void initData() {
        mMethodPresenter.setData();
        //1 子线程发送消息给本身
        new Thread() {
            public void run() {
                //子线程初始化Handler 必须Looper准备
                Looper.prepare();
                childHandler = new MyHandler(HandlerActivity.this, Looper.myLooper());
                Message message = Message.obtain();
                message.what = 0;
                message.obj = Thread.currentThread().getName() + ":线程发送的消息";
                childHandler.sendMessage(message);
                Looper.loop();
            }
        }.start();
    }

    /**
     * 内部类      handler正宗写法
     */
    class MyHandler extends Handler {
        // 弱引用 ，防止内存泄露
        private WeakReference<HandlerActivity> handlerActivityWeakReference = null;

        public MyHandler(HandlerActivity handlerActivity, Looper looper) {
            this.handlerActivityWeakReference = new WeakReference<HandlerActivity>(handlerActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            LogHelp.e("线程名字：-->>" + Thread.currentThread().getName());
            // 通过  软引用  看能否得到activity示例
            HandlerActivity handlerActivity = handlerActivityWeakReference.get();
            // 防止内存泄露
            if (null != handlerActivity && null != msg) {// 如果当前Activity，进行UI的更新
                if (msg.what == 0) {
                    sendMessageToMainThread(msg.obj.toString());
                } else if (msg.what == 1) {
                    LogHelp.e("消息内容：-->>" + msg.obj.toString());
                    sendMessageToChild(msg.obj.toString());
                } else if (msg.what == 2) {
                    LogHelp.e("消息内容：-->>" + msg.obj.toString());
                    text_handler_message.append("\n" + Thread.currentThread().getName() + msg.obj.toString());

                }
            } else {// 没有实例不进行操作

            }
        }

        private void sendMessageToMainThread(String msg) {
            LogHelp.e("消息内容：-->>" + msg);
            mainHandler = new MyHandler(HandlerActivity.this, getMainLooper());
            Message message = Message.obtain();
            message.what = 1;
            message.obj = Thread.currentThread().getName() + ":线程发送的消息";
            mainHandler.sendMessage(message);
            text_handler_message.append("\n" + Thread.currentThread().getName() + msg);
        }

        private void sendMessageToChild(String msg) {
            LogHelp.e("消息内容：-->>" + msg);
            Message message = Message.obtain();
            message.what = 2;
            message.obj = Thread.currentThread().getName() + ":线程发送的消息";
            childHandler.sendMessage(message);
            text_handler_message.append("\n" + Thread.currentThread().getName() + msg);
        }


    }


}
